#include "common.h"
#include "Sc.h"
#include "Scene.h"
#include "TreeSpec.h"
#include "Texture.h"
#include "Triangulator.h"

using namespace cg;

TreeSpecMan::TreeSpecMan()
{
	//birch 
	{
		birch.D = 0.01f;
		birch.infDistMultOfD = FLT_MAX;	
		birch.killDistMultOfD = 12.0f;
		birch.minArea = 0.000005f;
		birch.textParamX = 10.1f;	
		birch.textParamY = birch.D;
		birch.smoothLevel = 4;
		// envelope
		birch.envelopeRadius = 1.0f;		
		birch.envelopeCenter = Vec3::Y_AXIS * 2;
		birch.envelopeAtractionPoints = 5000;
		birch.textureName = "birch.tga";	
	}
	{
		tree1.D = 0.04f;
		tree1.infDistMultOfD = FLT_MAX;	
		tree1.killDistMultOfD = 10.0f;
		tree1.minArea = 0.00002f;
		tree1.textParamX = 10.1f;	
		tree1.textParamY = tree1.D;
		tree1.smoothLevel = 2;
		// envelope
		tree1.envelopeRadius = 1.0f;
		tree1.envelopeCenter = Vec3::Y_AXIS * 2;
		tree1.envelopeAtractionPoints = 1000;
		tree1.textureName = "bark1.tga";
	}
	{
		tree2.D = 0.04f;
		tree2.infDistMultOfD = 20;	
		tree2.killDistMultOfD = 4.0f;
		tree2.minArea = 0.00002f;
		tree2.textParamX = 10.1f;	
		tree2.textParamY = tree2.D;
		tree2.smoothLevel = 4;
		// envelope
		tree2.envelopeRadius = 1.5f;
		tree2.envelopeCenter = Vec3::Y_AXIS * 3;
		tree2.envelopeAtractionPoints = 1000;
		tree2.textureName = "bark1.tga";
	}

	{
		tree3.D = 0.04f;
		tree3.infDistMultOfD = 20;	
		tree3.killDistMultOfD = 2.0f;
		tree3.minArea = 0.00002f;
		tree3.textParamX = 10.1f;	
		tree3.textParamY = tree3.D;
		tree3.smoothLevel = 4;
		// envelope
		tree3.envelopeRadius = 1.5f;
		tree3.envelopeCenter = Vec3::Y_AXIS * 2;
		tree3.envelopeAtractionPoints = 5000;
		tree3.textureName = "bark1.tga";
	}
}

Entity* TreeSpecMan::GenerateTree( const TreeSpec& treeSpec, TextMan& textMan, const Mutator& mutator )
{
	Triangulator tri;
	Mesh* mesh;
	Entity *entity;
	float radius = treeSpec.envelopeRadius * mutator.scale;
	Vec3 center = treeSpec.envelopeCenter * mutator.scale;	
	BranchPoint::MinArea = treeSpec.minArea;
	Tree				tree;
	Envelope			envelope( radius, center, treeSpec.envelopeAtractionPoints);	
	//
	CreateTrunk(	tree, 
					treeSpec.D, 
					center.Len() - radius );
	//
	BuildTree(	envelope, 
				tree,
				treeSpec.D, 
				treeSpec.infDistMultOfD,
				treeSpec.killDistMultOfD,				
				treeSpec.smoothLevel );		
	// texture loading
	unsigned int textId;	
	textMan.Get( treeSpec.textureName, &textId );	
	mesh = tri.Triangulate( tree, treeSpec.textParamX, treeSpec.textParamY );
	entity = new Entity( mesh );	
	entity->material.SetTexture( textId );
	return entity;	
}